package com.aiit.controller;

import com.aiit.pojo.ADoctor;
import com.aiit.service.ADoctorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("doctor")
public class ADoctorController {

    @Resource
    private ADoctorService adoctorService;

    @GetMapping("selectList")
    public String selectList(Model model, @RequestParam(value = "pn", defaultValue = "1") int pn, @RequestParam(value = "size", defaultValue = "10") int size){
        PageHelper.startPage(pn, size);
        List<ADoctor> doctors = adoctorService.queryList();
        PageInfo<ADoctor> page = new PageInfo<>(doctors);
        model.addAttribute("page",page);
        return "doctor/doctor-list";
    }


    //点击[添加]，跳转到doctor-add页面
    @GetMapping("to-doctor-add")
    public String toDoctorAdd() {
        return "doctor/doctor-add";
    }


    @PostMapping("add")
    public String add(ADoctor doctor) {
        adoctorService.insert(doctor);
        return "redirect:/doctor/selectList";
    }

    //根据id删除用户，然后再次发送查询的请求
    @GetMapping("deleteById")
    public String deleteById(Integer id) {
        adoctorService.deleteById(id);
        return "redirect:/doctor/selectList";
    }


    //点击编辑，doctor-edit页面
    @GetMapping("to-doctor-edit/{id}")
    public String toAdminEdit(@PathVariable("id") Integer id, Model model) {
        ADoctor editdoctor = adoctorService.queryById(id);
        model.addAttribute("editdoctor", editdoctor);
        return "doctor/doctor-edit";
    }

    @PostMapping("update")
    public String update(ADoctor doctor) {
        adoctorService.update(doctor);
        return "redirect:/doctor/selectList";
    }

    @GetMapping("toSearchList/{kin}")
    public String toSearchList(@PathVariable("kin") String kin, Model model,@RequestParam(value = "pn", defaultValue = "1") int pn, @RequestParam(value = "size", defaultValue = "5") int size){
        String skind = kin;
        PageHelper.startPage(pn, size);
        List<ADoctor> doctors = adoctorService.queryByKind(kin);
        PageInfo<ADoctor> page = new PageInfo<>(doctors);
        model.addAttribute("page",page);
        return "doctor/doctor-list";
    }


    //通过主键查询单条数据
    @GetMapping("selectOne")
    public ADoctor selectOne(Integer id) {
        return this.adoctorService.queryById(id);
    }


}
